package com.weightx.weight.common;

import java.util.HashMap;

/**
 * 封装统一返回实体类
 * 继承HashMap 可随时put自定义key-value
 */
public class R extends HashMap<String, Object> {
    /**
     * 状态
     */
    public static final String STATUS_TAG = "success";
    /**
     * 状态码
     */
    public static final String CODE_TAG = "errCode";

    /**
     * 消息
     */
    public static final String MSG_TAG = "errMsg";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    public R() {
    }

    public R(int errCode, String errMsg) {
        super.put(STATUS_TAG, false);
        super.put(CODE_TAG, errCode);
        super.put(MSG_TAG, errMsg);
    }

    public R(Object obj) {
        super.put(STATUS_TAG, true);
        if (obj != null) {
            super.put(DATA_TAG, obj);
        }
    }

    public static R success() {
        return new R(null);
    }

    public static R success(Object obj) {
        return new R(obj);
    }

    public static R error() {
        return new R(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg());
    }

    public static R error(String msg) {
        return new R(ResultEnum.ERROR.getCode(), msg);
    }

    public static R error(Integer code, String msg) {
        return new R(code, msg);
    }

}
